package com.gongpingjialoan.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.CarBuyDetailActivity;
import com.gongpingjialoan.activity.car.MeetRecordActivity;
import com.gongpingjialoan.bean.MeetRecord;
import com.gongpingjialoan.utility.AbStrUtil;
import com.gongpingjialoan.utility.TimeUtils;
import com.gongpingjialoan.utility.Utils;

import java.util.List;

/**
 * Created by chuang on 2016/8/04.
 */
public class MeetRecordAdapter extends BaseExpandableListAdapter {


    private List<MeetRecord> mMeetRecordList;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    private Handler mHandler;

    public MeetRecordAdapter(List<MeetRecord> meetRecordList, Context context, Handler handler) {
        mMeetRecordList = meetRecordList;
        mContext = context;
        mHandler = handler;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void setData(List<MeetRecord> mMeetRecordList) {
        this.mMeetRecordList = mMeetRecordList;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return mMeetRecordList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mMeetRecordList.get(groupPosition).appointList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mMeetRecordList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mMeetRecordList.get(groupPosition).appointList;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        ParentViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.meet_record_parent_item, null);
            holder = new ParentViewHolder();
            holder.timeTv = (TextView) convertView.findViewById(R.id.time);
            holder.timeTitletv = (TextView) convertView.findViewById(R.id.time_title);
            holder.timeIm = (TextView) convertView.findViewById(R.id.time_tv);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        if ("vip".equals(mMeetRecordList.get(groupPosition).type)) {
            holder.timeTv.setText(TimeUtils.timeChineseFormat(mMeetRecordList.get(groupPosition).date_time));
            holder.timeTv.setVisibility(View.VISIBLE);
            holder.timeTitletv.setVisibility(View.VISIBLE);
            holder.timeIm.setVisibility(View.VISIBLE);
        } else {
            holder.timeTv.setVisibility(View.GONE);
            holder.timeTitletv.setVisibility(View.GONE);
            holder.timeIm.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.meet_record_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.pic);
            holder.tvCarName = (TextView) convertView.findViewById(R.id.carName);
            holder.tvCarTitle = (TextView) convertView.findViewById(R.id.cartitle);
            holder.mileTextView = (TextView) convertView.findViewById(R.id.mile);
            holder.priceTextView = (TextView) convertView.findViewById(R.id.price);
            holder.bargainAddress = (TextView) convertView.findViewById(R.id.bargain_address);
            holder.evalPrice = (TextView) convertView.findViewById(R.id.eval_price);
            holder.bargainResult = (ImageView) convertView.findViewById(R.id.bargain_result);
            holder.phoneIm =  convertView.findViewById(R.id.phone_im);
            holder.hadsellTv = (TextView) convertView.findViewById(R.id.had_sell_tv);
            holder.caridTv = (TextView) convertView.findViewById(R.id.car_id_tv);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }


        final MeetRecord.Appoint appoint = mMeetRecordList.get(groupPosition).appointList.get(childPosition);
        if (appoint != null) {

            if ("review".equals(appoint.car_status)) {
                holder.bargainResult.setVisibility(View.GONE);
                holder.hadsellTv.setText("已下架");
                holder.hadsellTv.setVisibility(View.VISIBLE);
            } else {
                if ("S".equals(appoint.status)) {
                    holder.hadsellTv.setText("预约成功");
                    holder.hadsellTv.setVisibility(View.VISIBLE);
                    holder.caridTv.setVisibility(View.GONE);
                } else {
                    holder.hadsellTv.setVisibility(View.GONE);
                    holder.caridTv.setVisibility(View.VISIBLE);
                    holder.caridTv.setText("ID: " + appoint.car_id);
                }
                holder.bargainResult.setVisibility(View.VISIBLE);
                holder.bargainResult.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Message msg = Message.obtain();
                        msg.what = MeetRecordActivity.CANCEL_MEET;
                        msg.arg1 = appoint.id;
                        msg.obj = mMeetRecordList.get(groupPosition).type;
                        mHandler.sendMessage(msg);
                    }
                });
            }

            holder.phoneIm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "4000253500"));
                    mContext.startActivity(intent);
                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, CarBuyDetailActivity.class);
                    intent.putExtra("typevalue", String.valueOf(appoint.car_id));
                    intent.putExtra("from", "appointRecord");
                    mContext.startActivity(intent);
                }
            });
            holder.tvCarName.setText(appoint.model_zh);
            if (TextUtils.isEmpty(appoint.model_detail_zh)) {
                holder.tvCarTitle.setText(appoint.title);
            } else {
                holder.tvCarTitle.setText(appoint.model_detail_zh);
            }

            holder.mileTextView.setText(appoint.mile + "万公里" + "/" + appoint.year + "年上牌/" + appoint.city);
            holder.priceTextView.setText(appoint.price);
            if ("".equals(appoint.eval_price) || Utils.isLing(appoint.eval_price)) {
                holder.evalPrice.setText("");
            }else{
                String comparePrice = "与公平价估值一致";
                double priceCompare = AbStrUtil.get2Double(Double.parseDouble(appoint.price) - Double.parseDouble(appoint.eval_price));
                if (priceCompare < 0) {
                    double priceCompare1 = -priceCompare;
                    comparePrice = "(低于公平价估值" + priceCompare1 + "万)";
                } else if (priceCompare > 0) {
                    comparePrice = "(高于公平价估值" + priceCompare + "万)";
                }
                holder.evalPrice.setText(comparePrice);
            }

            if (TextUtils.isEmpty(appoint.appoint_address)) {
                holder.bargainAddress.setText(R.string.default_address);
                holder.bargainAddress.setTextColor(mContext.getResources().getColor(R.color.text_orange_dark));
            } else {
                holder.bargainAddress.setText(appoint.appoint_address);
                holder.bargainAddress.setTextColor(mContext.getResources().getColor(R.color.text_grey_dark));
            }

            Glide.with(mContext).load(appoint.thumbnail).placeholder(R.drawable.car_loading).centerCrop().crossFade().into(holder.imgThumbnail);
        }
        return convertView;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public static class ViewHolder {
        public ImageView imgThumbnail;

        public TextView tvCarName;

        public TextView tvCarTitle;

        public TextView mileTextView;

        public TextView priceTextView;

        public TextView bargainAddress;

        public TextView evalPrice;

        public ImageView bargainResult;

        public View phoneIm;

        public TextView hadsellTv;

        public TextView caridTv;

    }

    public static class ParentViewHolder {
        public TextView timeTv;
        public TextView timeTitletv;
        public TextView timeIm;

    }
}

